/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import BusinessLogic.Constants;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author James
 */
public class Utility {
    //Private methods of Partner Bean:
    public static String getUniqueID() {
        int NUM_CHARS = 20;
        String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random r = new Random();
        char[] buf = new char[NUM_CHARS];

        for (int i = 0; i < buf.length; i++) {
            buf[i] = chars.charAt(r.nextInt(62));
        }

        return new String(buf);
    }
    
    public static String[] getMultipartFormFieldParameterValues(HttpServletRequest request, String fieldName) throws FileUploadException {
        List<String> parameters = new LinkedList<String>();
       
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload    upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        Iterator<FileItem> iter = items.iterator();
   
        while (iter.hasNext()) {
            FileItem item    = iter.next();
            if (item.isFormField() && fieldName.equals(fieldName)) parameters.add(item.getString());
        }
        
        return parameters.toArray(new String[0]);
    }
    
    public static HashMap getMultipartFileParametersWithPrefix(HttpServletRequest request, String prefix) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        Iterator<FileItem> iter = items.iterator();
   
        while (iter.hasNext()) {
            FileItem item    = iter.next();
            String fieldName = item.getFieldName();
            if (!item.isFormField() && !fieldName.equals(prefix) && fieldName.contains(prefix)) map.put(fieldName, item);
        }
        
        return map;
    }
    
    public static HashMap getOptionalMultipartFileParameters(HttpServletRequest request, String... args) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
   
        for (String neededField : args) {
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if (!item.isFormField() && item.getFieldName().equals(neededField)) {
                    map.put(neededField, item);
                    iter.remove();
                    break;
                }
            }
        }
        
        return map;
    }
    
    public static HashMap getCompulsoryMultipartFileParameters(HttpServletRequest request, String... args) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
   
        for (String neededField : args) {
            Iterator<FileItem> iter = items.iterator();
            boolean found = false;
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if (!item.isFormField() && item.getFieldName().equals(neededField)) {
                    map.put(neededField, item);
                    iter.remove();
                    found = true;
                    break;
                }
            }
            // If required field not found or contain empty string, return empty HashMap
            if (!found) {
                map.clear();
                break;
            }
        }
        
        return map;
    }
    
    public static HashMap getAllMultipartFileParameters(HttpServletRequest request) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        Iterator<FileItem> iter = items.iterator();
   
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (!item.isFormField()) map.put(item.getFieldName(), item);
        }
        
        return map;
    }
    
    public static HashMap getMultipartFormFieldParametersWithPrefix(HttpServletRequest request, String prefix) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload    upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        Iterator<FileItem> iter = items.iterator();
   
        while (iter.hasNext()) {
            FileItem item    = iter.next();
            String fieldName = item.getFieldName();
            if (item.isFormField() && !fieldName.equals(prefix) && fieldName.startsWith(prefix)) map.put(fieldName, item.getString());
        }
        
        return map;
    }
    
    public static HashMap getOptionalMultipartFormFieldParameters(HttpServletRequest request, String... args) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        
        for (String neededField : args) {
            Iterator<FileItem> iter = items.iterator();
            boolean found = false;
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if (item.isFormField() && item.getFieldName().equals(neededField)) {
                    map.put(neededField, item.getString().trim());
                    iter.remove();
                    found = true;
                    break;
                }
            }
            // If required field not found or contain empty string, return empty HashMap
            if (!found) {
                map.put(neededField, "");
                break;
            }
        }
        
        return map;
    }
    
    public static HashMap getCompulsoryMultipartFormFieldParameters(HttpServletRequest request, String... args) throws FileUploadException {
        HashMap map = new HashMap();
        
        File              directory = new File(Constants.FOLDER_TEMP);
        DiskFileItemFactory factory = new DiskFileItemFactory(Constants.FILE_UPLOAD_MAX_SIZE+5000, directory);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);
        
        for (String neededField : args) {
            Iterator<FileItem> iter = items.iterator();
            boolean found = false;
            while (iter.hasNext()) {
                FileItem item = iter.next();
                if (item.isFormField() && item.getFieldName().equals(neededField)) {
                    String value = item.getString().trim();
                    if (!value.equals("")) {
                        map.put(neededField, value);
                        iter.remove();
                        found = true;
                        break;
                    }
                }
            }
            // If required field not found or contain empty string, return empty HashMap
            if (!found) {
                map.clear();
                break;
            }
        }
        
        return map;
    }
    
    public static HashMap getOptionalParameters(HttpServletRequest request, String... args) {
        HashMap map = new HashMap();
        
        for (String fieldName : args) {
            String value = request.getParameter(fieldName);
            if (value == null) map.put(fieldName, "");
            else map.put(fieldName, value.trim());
        }
        
        return map;
    }
    
    public static HashMap getCompulsoryParameters(HttpServletRequest request, String... args) {
        HashMap map = new HashMap();
        
        for (String fieldName : args) {
            String value = request.getParameter(fieldName);
            if (value == null || (value = value.trim()).equals("")) {
                map.clear();
                break;
            } else map.put(fieldName, value);
        }
        
        return map;
    }
}
